using AppToolKit.Business;
using AppToolKit.Data;
using AppToolKit.Data.NHibernate;
using NHibernate;
using Spring.Context.Support;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
using System;
namespace AppToolKit.Core.Context.Spring.AutoConstruction.Builder
{
	public class GenericeBusinessServerBuilder : ServerBuilderBase
	{
		private GenericeBusinessSupport support_ = new GenericeBusinessSupport();
		protected override string GetBusinessName(Type type)
		{
			Type entityType = this.support_.GetEntityType(type);
			return this._GetBusinessName(entityType);
		}
		private string _GetBusinessName(Type entityType)
		{
			return BusinessNameSymbol.GetBusinessName(entityType.FullName);
		}
		public override bool IsSupport(Type type)
		{
			return this.support_.IsSupport(type);
		}
		protected override void RegisterBusiness(Type businessType)
		{
			Type entityType = this.support_.GetEntityType(businessType);
			string daoName = this.GetDaoName(entityType);
			string text = this._GetBusinessName(entityType);
			DefaultListableObjectFactory defaultListableObjectFactory = (DefaultListableObjectFactory)((XmlApplicationContext)base.ApplicationContext).ObjectFactory;
			Type typeFromHandle = typeof(IBusiness<>);
			Type interfaceType = typeFromHandle.MakeGenericType(new Type[]
			{
				entityType
			});
			Type type;
			if (base.ConstructionLocation.ContainImplement(interfaceType))
			{
				type = base.ConstructionLocation.GetImplementType(interfaceType);
			}
			else
			{
                Type typeFromHandle2 = typeof(DefaultBusiness<>);
				Type type2 = typeFromHandle2.MakeGenericType(new Type[]
				{
					entityType
				});
				type = type2;
			}
			Type typeFromHandle3 = typeof(IDataAccessObject<>);
			Type interfaceType2 = typeFromHandle3.MakeGenericType(new Type[]
			{
				entityType
			});
			Type type3;
			if (base.ConstructionLocation.ContainImplement(interfaceType2))
			{
				type3 = base.ConstructionLocation.GetImplementType(interfaceType);
			}
			else
			{
				Type typeFromHandle4 = typeof(DefaultDataAccess<>);
				Type type4 = typeFromHandle4.MakeGenericType(new Type[]
				{
					entityType
				});
				type3 = type4;
			}
			RootObjectDefinition rootObjectDefinition = new RootObjectDefinition(type3);
			rootObjectDefinition.IsSingleton=false;
			rootObjectDefinition.PropertyValues.Add("NativeDatabase", new RuntimeObjectReference("dbProcessor"));
            rootObjectDefinition.PropertyValues.Add("SessionFactory", new RuntimeObjectReference(this.helper_.GetObjectName(typeof(ISessionFactory))));
			defaultListableObjectFactory.RegisterObjectDefinition(daoName, rootObjectDefinition);
			RootObjectDefinition rootObjectDefinition2 = new RootObjectDefinition(type);
            rootObjectDefinition2.IsSingleton = false;
            rootObjectDefinition2.PropertyValues.Add("Dao", new RuntimeObjectReference(daoName));
            rootObjectDefinition2.PropertyValues.Add("Support", new RuntimeObjectReference(this.helper_.GetObjectName(typeof(BusinessSupport))));
			Type typeFromHandle5 = typeof(IEntityFactory<>);
			Type type5 = typeFromHandle5.MakeGenericType(new Type[]
			{
				entityType
			});
			if (base.ConstructionLocation.ContainImplement(entityType))
			{
				Type implementType = base.ConstructionLocation.GetImplementType(entityType);
				RootObjectDefinition rootObjectDefinition3 = new RootObjectDefinition(implementType);
				rootObjectDefinition3.IsSingleton=false;
				string factoryName = this.GetFactoryName(entityType);
				defaultListableObjectFactory.RegisterObjectDefinition(factoryName, rootObjectDefinition3);
				rootObjectDefinition2.PropertyValues.Add("Factory", new RuntimeObjectReference(factoryName));
			}
			defaultListableObjectFactory.RegisterObjectDefinition(text, rootObjectDefinition2);
		}
		private string GetFactoryName(Type entityType)
		{
			return entityType.FullName + "_factory";
		}
		private string GetDaoName(Type entityType)
		{
			return entityType.FullName + "_dao";
		}
	}
}
